﻿Public Class FrmNhomKH
    Dim provider As New DataProvider
    Dim flag As String = 0
    Function LoadDuLieu()
        dtgnhomkh.DataSource = New DataTable
        Dim sql As String
        sql = "select MaNhomKH,TenNhomKH from NhomKH"
        Dim dt As New DataTable
        dt = provider.ExecuteQuery(sql)
        dtgnhomkh.DataSource = dt
        Enable(False)
        btnthem.Enabled = True
        btnluu.Enabled = False
        btnsua.Enabled = True
        btnxoa.Enabled = True
        dtgnhomkh.Enabled = True
        btnhuy.Enabled = False


    End Function
    Private Sub FrmNhomKH_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        Enable(True)
        btnsua.Enabled = False
        btnxoa.Enabled = False
        btnthem.Enabled = False
        dtgnhomkh.Enabled = False
        txtmanhomkh.Text = ""
        txttennhomkh.Text = ""
        btnhuy.Enabled = True


    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        Try
            Dim row As Integer = dtgnhomkh.CurrentCell.RowIndex
            Dim MaNhomKH As String
            MaNhomKH = dtgnhomkh.Rows(row).Cells(0).Value
            Dim sql As String
            sql = " DELETE FROM NhomKH where MaNhomKH = '" + MaNhomKH + "'"
            provider.ExecuteNonQuery(sql)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        btnhuy.Enabled = False
        If txtmanhomkh.Text = "" Then
            MessageBox.Show("Chọn 1 bộ phận để sửa ")
        Else
            flag = 2
            Enable(True)
            btnthem.Enabled = False
            btnxoa.Enabled = False
            btnsua.Enabled = False
            txtmanhomkh.Enabled = False

        End If
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try

            If flag = 1 Then
                Dim MaNhomKH As String
                Dim TenNhomKH As String
                MaNhomKH = txtmanhomkh.Text
                TenNhomKH = txttennhomkh.Text
                Dim sql As String
                sql = "select * from NhomKH where MaNhomKH='" + MaNhomKH + "'"
                Dim dt As New DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã Nhóm Khách Hàng Trùng")
                    txtmanhomkh.Focus()
                Else
                    sql = "insert into NhomKH values(N'" + MaNhomKH + "',N'" + TenNhomKH + "')"
                    provider.ExecuteNonQuery(sql)
                    LoadDuLieu()
                End If


            ElseIf flag = 2 Then

                Dim MaNhomKH As String
                Dim TenNhomKH As String
                MaNhomKH = txtmanhomkh.Text
                TenNhomKH = txttennhomkh.Text
                Dim sql As String
                sql = "update NhomKH set MaNhomKH = '" + MaNhomKH + "', TenNhomKH= N'" + TenNhomKH + "' WHERE MaNhomKH = '" + MaNhomKH + "'"
                provider.ExecuteNonQuery(sql)
                LoadDuLieu()
            End If

        Catch ex As Exception
            MessageBox.Show("Lỗi")
        End Try
    End Sub
    Private Sub Enable(ByVal i As Boolean)
        txtmanhomkh.Enabled = i
        txttennhomkh.Enabled = i
        btnluu.Enabled = i

    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()
    End Sub

    Private Sub dtgnhomkh_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgnhomkh.CellClick
        Try
            Dim row As Integer = dtgnhomkh.CurrentCell.RowIndex
            txtmanhomkh.Text = dtgnhomkh.Rows(row).Cells(0).Value
            txttennhomkh.Text = dtgnhomkh.Rows(row).Cells(1).Value
        Catch ex As Exception

        End Try
    End Sub
End Class